package com.danhuang.cloud.danhuangdaily.service.Impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.danhuang.cloud.danhuangcore.constant.ResConstant;
import com.danhuang.cloud.danhuangcore.model.DhCommemorationInfo;
import com.danhuang.cloud.danhuangcore.model.DhRes;
import com.danhuang.cloud.danhuangdaily.mapper.DhCommemorationInfoMapper;
import com.danhuang.cloud.danhuangdaily.service.CommemorationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * @author huangwubin
 */
@Service
@Slf4j
public class CommemorationServiceImpl implements CommemorationService {

    @Resource
    private DhCommemorationInfoMapper dhCommemorationInfoMapper;

    @Override
    public DhRes info() {
        try{
            List<DhCommemorationInfo> list = dhCommemorationInfoMapper.selectList(
                    new LambdaQueryWrapper<DhCommemorationInfo>().eq(DhCommemorationInfo::getDelFlg, 0).orderByAsc(DhCommemorationInfo::getOrderBy));

            List<Map<String, String>> result = new ArrayList<>();
            list.forEach(e-> {
                Map<String, String> map = new HashMap<>();
                map.put("name", e.getCommemorationName());
                long betweenDay = DateUtil.between(e.getCommemorationDate(), new Date(), DateUnit.DAY);
                map.put("distance", String.valueOf(betweenDay));
                map.put("check", "one");
                if (e.getCommemorationDate().compareTo(new Date()) < 0){
                    map.put("check", "two");
                }
                map.put("date", DateUtil.format(e.getCommemorationDate(), DatePattern.CHINESE_DATE_FORMAT));
                map.put("bgColor", e.getBgColor());
                result.add(map);
            });

            return DhRes.success(result);
        }catch (Exception e){
            log.error("查询纪念日异常",e);
            return DhRes.fail(ResConstant.SYSTEM_ERROR_CODE);
        }
    }
}
